package com.itheima.manager_system.teacher_service;

import com.itheima.manager_system.domain.Person;
import com.itheima.manager_system.domain.Teacher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class TeacherOperater {
    private Scanner sc = new Scanner(System.in);
    
    public void InfoMenu(ArrayList<Person> list){
        String select;
        loopStudent:
        while (true) {
            System.out.println("--------------------------------------------------");
            System.out.println("1:添加老师\t2:删除老师\t3:修改老师\t4:查询所有老师\t\n5:查询老师的名字\t6:返回上一层");
            System.out.println("--------------------------------------------------");
            System.out.println("请选择:");
            select = sc.next();
            switch (select) {
                case "1":
                    addTeacher(list);
                    break;
                case "2":
                    removeTeacher(list);
                    break;
                case "3":
                    updateTeacher(list);
                    break;
                case "4":
                    allTeacher(list);
                    break ;
                case "5":
                    sameName(list);
                    break;
                case "6":
                    //返回上一层
                    break loopStudent;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }

    }
    //通过名字查找
    private void sameName(ArrayList<Person> list) {
        if(list.isEmpty()){
            System.out.println("暂无可供查询的信息!");
            return;
        }
        System.out.println("请输入要查询的名字:");
        String queryName = sc.next();
        ArrayList<Person> sameNameList = new ArrayList<>();
        for (Person student : list) {
            if (!"000".equals(student.getId())) {
                if (queryName.equals(student.getName())) {
                    sameNameList.add( student);
                }
            }
        }
        if (sameNameList.isEmpty()) {     //新集合为空则信息查不到
            System.out.println("查询不到" + queryName + "的信息!");
        } else {                       //否则打印新集合中的信息
            System.out.println("--------------------------------------------------");
            System.out.println("编号\t\t姓名\t\t年龄\t\t住址\t\t职业");
            for (Person p : sameNameList) {
                System.out.println(p.getId() + "\t\t" + p.getName() + "\t\t" + p.getAge() + "\t\t" + p.getAdress()+ "\t\t"+p.getJob());
            }
        }
    }
    //查
    private void allTeacher(ArrayList<Person> list) {
        if (list.isEmpty()) {
            System.out.println("暂无可供查询的信息!");
        } else {
            sortList(list);
            System.out.println("--------------------------------------------------");
            System.out.println("编号\t\t姓名\t\t年龄\t\t住址\t\t职业");
            for (Person p : list) {

                if (p.getId()!=0) {
                    System.out.println(p.getId() + "\t\t" + p.getName() + "\t\t" + p.getAge() + "\t\t" + p.getAdress()+ "\t\t"+p.getJob());
                }
            }
        }
    }
    //改
    private void updateTeacher(ArrayList<Person> list) {
        if(list.size()==1){
            System.out.println("暂无可供修改的信息!");
            return;
        }
        System.out.println("请输入要修改的编号:");
        int updateId = sc.nextInt();
        int index = getIndex(list, updateId);
        if (index == -1) {
            System.out.println("编号" + updateId + "不存在!");

        } else {
            System.out.println("请输入修改的名字:");
            String updateName = sc.next();
            System.out.println("请输入修改的年龄:");
            int updateAge = sc.nextInt();
            System.out.println("请输入修改的住址:");
            String updateAdress = sc.next();

            Person updateTeacher = list.get(index);
            updateTeacher.setName(updateName);
            updateTeacher.setAge(updateAge);
            updateTeacher.setAdress(updateAdress);

            System.out.println("修改成功");

        }
    }
    //删
    private void removeTeacher(ArrayList<Person> list) {
        if(list.isEmpty()){
            System.out.println("暂无可供删除的信息!");
            return;
        }
        System.out.println("请输入要删除的编号:");
        int removeId = sc.nextInt();
        int index = getIndex(list, removeId);
        if (index == -1) {
            System.out.println("编号" + removeId + "不存在!");
        } else {
            System.out.println("编号"+removeId+"\t"+list.get(index).getName()+"删除成功");
            list.remove(index);

        }
    }
    //增
    private void addTeacher(ArrayList<Person> list) {
        while (true) {
            System.out.println("请输入要添加的编号:");
            int addId = sc.nextInt();
            int index = getIndex(list, addId);
            if (index != -1) {
                System.out.println("编号" + addId + "已存在!");
            } else {
                System.out.println("请输入添加的名字:");
                String addName = sc.next();
                System.out.println("请输入添加的年龄:");
                int addAge = sc.nextInt();
                System.out.println("请输入添加的住址:");
                String addAdress = sc.next();
                Teacher addTeacher = new Teacher();
                addTeacher.setId(addId) ;
                addTeacher.setName(addName);
                addTeacher.setAge(addAge);
                addTeacher.setAdress(addAdress);
                addTeacher.setJob("teacher");

                list.add(addTeacher);
                System.out.println("添加成功");
                break;
            }
        }
    }

    //编号所在元素在集合中的索引(不存在则返回-1)
    public  int getIndex(ArrayList<Person> list, int id) {
            /*
            编号唯一性的判断方法
            遍历list,用id逐个匹配list中每个元素的id,如果匹配到就返回索引
            如果没有匹配到,最后返回-1
             */
        for (int i = 0; i < list.size(); i++) {
            if (id==list.get(i).getId()) {
                return i;
            }
        }
        return -1;
    }

    //按id从小到大排序集合
    public  void  sortList(ArrayList<Person> list) {
        ArrayList<Person> list2 = new ArrayList<>();
        ArrayList<Integer> id = new ArrayList<>();
        for (Person i : list) {
            id.add(i.getId());
        }
        Collections.sort(id);
        for (Integer i : id) {
            int index = getIndex(list, i);
            list2.add(list.get(index));
        }
        list.clear();
        for (Person p : list2) {
            list.add(p);
        }
    }

    
}
